package com.hp.attendance.serlvet.attendance;

import com.hp.attendance.entity.Attendance;
import com.hp.attendance.entity.dto.AttendanceDto;
import com.hp.attendance.entity.dto.ExcelDTO;
import com.hp.attendance.service.AttendanceService;
import com.hp.attendance.service.ExcelService;
import com.hp.attendance.util.ExcelUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/exportExcel")
public class ExportExcelServlet extends HttpServlet {
    private AttendanceService attendanceService=new AttendanceService();

    private ExcelService excelService = new ExcelService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //数据导出功能
        try {
            //查询所有的考勤数据
            List<Attendance> list = attendanceService.listAttendance2();

            List<ExcelDTO> listExcel = excelService.getList("attendance");
            //考勤数据要存入到excel文档中去
            ExcelUtil.exportExcel(list,listExcel,resp,"考勤信息统计.xlsx");
            /*//1.创建excel文档对象
            XSSFWorkbook sheets = new XSSFWorkbook();
            //2.创建工作表对象
            XSSFSheet sheet = sheets.createSheet();
            //3.创建行对象
            XSSFRow row = sheet.createRow(0);
            //4.创建单元格对象
            //5.往单元格写数据
            row.createCell(0).setCellValue("序号");
            row.createCell(1).setCellValue("员工编号");
            row.createCell(2).setCellValue("日期");
            row.createCell(3).setCellValue("打卡时间段");
            row.createCell(4).setCellValue("上班打卡时间");
            row.createCell(5).setCellValue("上班打卡状态");
            row.createCell(6).setCellValue("下班打卡时间");
            row.createCell(7).setCellValue("下班打卡状态");
            row.createCell(8).setCellValue("工作类型");
            row.createCell(9).setCellValue("备注");

            //遍历list
            for (int i = 0; i < list.size(); i++) {
                //创建新的一行
                XSSFRow row1 = sheet.createRow(i + 1);
                AttendanceDto att = list.get(i);
                row1.createCell(0).setCellValue(i+1);
                if (att.getAttendance().getEmployee_number()!=null){
                    row1.createCell(1).setCellValue(att.getAttendance().getEmployee_number());
                }
                if (att.getAttendance().getDay()!=null){
                    Date day = att.getAttendance().getDay();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String format = sdf.format(day);
                    row1.createCell(2).setCellValue(format);
                }
                if (att.getAttendance().getTime_type()!=null){
                    row1.createCell(3).setCellValue(att.getAttendance().getTime_type());
                }
                SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
                if (att.getAttendance().getStart_time()!=null){
                    row1.createCell(4).setCellValue(sdf1.format(att.getAttendance().getStart_time()));
                }
                row1.createCell(5).setCellValue(att.getAttendance().getStart_type());
                if (att.getAttendance().getEnd_time()!=null){
                    row1.createCell(6).setCellValue(sdf1.format(att.getAttendance().getEnd_time()));
                }
                row1.createCell(7).setCellValue(att.getAttendance().getEnd_type());
                row1.createCell(8).setCellValue(att.getAttendance().getWork_type());
                row1.createCell(9).setCellValue(att.getAttendance().getNotes());
            }

            //6.把excel文档以附件的形式下载
            //设置excel文件格式
            resp.setContentType("application/vnd.ms-excel");
            //设置文件以附件形式下载
            String filename =new String("考勤信息统计.xlsx".getBytes(),"ISO-8859-1");
            resp.setHeader("Content-Disposition","attachment;filename="+filename);
            sheets.write(resp.getOutputStream());*/
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
